package com.vanchu.dropdownviewsample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vanchu.dropdownview.Dp2Px;
import com.vanchu.dropdownview.component.ListDropDownView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private ImageView imgShowStrAndIcon;

    private ImageView imgShowOnlyText;

    private ImageView imgWithoutXml;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgShowOnlyText = (ImageView) findViewById(R.id.main_page_img_more_only_text);
        imgShowStrAndIcon = (ImageView) findViewById(R.id.main_page_img_more_str_and_icon);
        imgWithoutXml = (ImageView) findViewById(R.id.main_page_img_more_no_xml);

        imgShowOnlyText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDropDownViewOnlyText();
            }
        });

        imgShowStrAndIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showListDropDownView();
            }
        });

        imgWithoutXml.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDropDownViewWithoutXml();
            }
        });
    }

    private void showDropDownViewWithoutXml() {
        ListDropDownView.Builder<StrAndIconData> builder = new ListDropDownView.Builder<>(imgWithoutXml);
        ListDropDownView.Config config = new ListDropDownView.Config(
                R.drawable.bg_more_option, (int) Dp2Px.dp2px(this, 126),
                new ListDropDownView.Config.Padding(0, 8, 0, 0)
        );
        builder.setConfig(config);
        builder.setItemClickListener(new ListDropDownView.ItemClickListener<StrAndIconData>() {
            @Override
            public boolean onItemClick(int position, StrAndIconData data) {
                if (data.iconRes == R.drawable.ic_other_apps) {
                    jumpToMoreApps();
                    return false;
                }
                if (data.iconRes == R.drawable.ic_mortgage_knowledge) {
                    jump2HelpPage();
                    return false;
                }
                if (data.iconRes == R.drawable.ic_share_to_friends) {
                    prepareShare();
                    return false;
                }
                return false;
            }
        });
        builder.setDropItemAdapter(new ListDropDownView.DropItemAdapter<StrAndIconData>(buildStrAndIcons()) {
            @Override
            public int getItemLayoutId() {
                return R.layout.item_drop_down_view;
            }

            @Override
            public void bind(View itemView, StrAndIconData data) {
                ImageView img = (ImageView) itemView.findViewById(R.id.item_more_opts_ic);
                TextView textView = (TextView) itemView.findViewById(R.id.item_more_opts_tv);
                img.setImageResource(data.iconRes);
                textView.setText(data.text);
            }
        });
        builder.create().show(5, 10);
    }

    private void showDropDownViewOnlyText() {
        ListDropDownView.Builder<String> builder = new ListDropDownView.Builder<>(imgShowOnlyText);
        ListDropDownView.Config config = new ListDropDownView.Config(
                R.drawable.bg_only_text, 100,
                new ListDropDownView.Config.Padding(0, 0, 0, 0)
        );
        builder.setConfig(config);
        builder.setItemClickListener(new ListDropDownView.ItemClickListener<String>() {
            @Override
            public boolean onItemClick(int position, String data) {
                tips(data);
                return false;
            }
        });
        builder.setDropItemAdapter(new ListDropDownView.DropItemAdapter<String>(getOnlyTextData()) {

            @Override
            public int getItemLayoutId() {
                return R.layout.item_more_opt_only_text;
            }

            @Override
            public void bind(View itemView, String data) {
                TextView textView = (TextView) itemView.findViewById(R.id.only_text_tv);
                textView.setText(data);
            }
        });
        ListDropDownView listDropDownView = builder.create();
        listDropDownView.show(0, 10);
    }

    private void showListDropDownView() {
        final List<StrAndIconData> strAndIconDatas = buildStrAndIcons();
        ListDropDownView.Builder<StrAndIconData> builder = new ListDropDownView.Builder<>(imgShowStrAndIcon);
        ListDropDownView.Config config = new ListDropDownView.Config(
                R.drawable.bg_more_option, (int) Dp2Px.dp2px(this, 126),
                new ListDropDownView.Config.Padding(0, 8, 0, 0)
        );
        builder.setConfig(config);
        builder.setItemClickListener(new ListDropDownView.ItemClickListener<StrAndIconData>() {
            @Override
            public boolean onItemClick(int position, StrAndIconData data) {
                if (data.iconRes == R.drawable.ic_other_apps) {
                    jumpToMoreApps();
                    return false;
                }
                if (data.iconRes == R.drawable.ic_mortgage_knowledge) {
                    jump2HelpPage();
                    return false;
                }
                if (data.iconRes == R.drawable.ic_share_to_friends) {
                    prepareShare();
                    return false;
                }
                return false;
            }
        });
        builder.setDropItemAdapter(new ListDropDownView.DropItemAdapter<StrAndIconData>(strAndIconDatas) {
            @Override
            public int getItemLayoutId() {
                return R.layout.item_drop_down_view;
            }

            @Override
            public void bind(View itemView, StrAndIconData data) {
                ImageView img = (ImageView) itemView.findViewById(R.id.item_more_opts_ic);
                TextView textView = (TextView) itemView.findViewById(R.id.item_more_opts_tv);
                img.setImageResource(data.iconRes);
                textView.setText(data.text);
            }
        });
        ListDropDownView listDropDownView = builder.create();
        listDropDownView.show(5, 10);
    }

    class StrAndIconData {
        final String text;

        final int iconRes;

        StrAndIconData(String text, int iconRes) {
            this.text = text;
            this.iconRes = iconRes;
        }
    }

    @NonNull
    private List<StrAndIconData> buildStrAndIcons() {
        final List<StrAndIconData> strAndIconDatas = new ArrayList<>();
        strAndIconDatas.add(new StrAndIconData(getString(R.string.more_apps), R.drawable.ic_other_apps));
        strAndIconDatas.add(new StrAndIconData(getString(R.string.mortgage_knowledge), R.drawable.ic_mortgage_knowledge));
        strAndIconDatas.add(new StrAndIconData(getString(R.string.share_to_friends), R.drawable.ic_share_to_friends));
        return strAndIconDatas;
    }

    @NonNull
    private List<String> getOnlyTextData() {
        List<String> datas = new ArrayList<>();
        datas.add("opt1");
        datas.add("opt2");
        datas.add("opt3");
        return datas;
    }

    private void tips(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private void jump2HelpPage() {
        Toast.makeText(this, "跳转到帮助页面", Toast.LENGTH_SHORT).show();
    }

    private void prepareShare() {
        Toast.makeText(this, "准备分享", Toast.LENGTH_SHORT).show();
    }

    private void jumpToMoreApps() {
        Toast.makeText(this, "跳转到更多应用", Toast.LENGTH_SHORT).show();
    }
}